package com.lzs.baseruku.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lzs.baseruku.mapper.PurchaseMapper;
import com.lzs.baseruku.service.PurchaseService;
import com.lzs.entity.Purchase;
import com.lzs.util.R;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ymx
 * @since 2020-11-24
 */
@Service
public class PurchaseServiceImpl extends ServiceImpl<PurchaseMapper, Purchase> implements PurchaseService {
    @Autowired
    private PurchaseMapper purchaseMapper;
    @Override
    public Page<Purchase> selectPurchase(Integer page, Integer limit, Purchase purchase) {
        Page<Purchase> pageInfo=new Page<>(page,limit);
        String purchaseno=purchase.getPurchaseno();
        String suppliername=purchase.getSuppliername();
        String state=purchase.getState();
        QueryWrapper queryWrapper=new QueryWrapper();
        if (StringUtils.isNotEmpty(purchaseno)){
            queryWrapper.like("purchaseno",purchaseno);
        }
        if (StringUtils.isNotEmpty(suppliername)){
            queryWrapper.like("suppliername",suppliername);
        }
        if (StringUtils.isNotEmpty(state)){
            queryWrapper.like("state",state);
        }
        this.baseMapper.selectPage(pageInfo,queryWrapper);
        return pageInfo;
    }

    @Override
    public R updateState(List<Long> ids, String state) {
        int i=purchaseMapper.updateState(ids,state);
        if (i>0){
            return R.ok().message("操作成功");
        }else {
            return R.error().message("操作失败");
        }

    }

    @Override
    public R updateStateByPurchaseno(String purchaseno) {
        int i=purchaseMapper.updateStateByPurchaseno(purchaseno);
        if (i>0){
            return R.ok().message("操作成功");
        }else {
            return R.error().message("操作失败");
        }
    }
}
